package com.dingyuhang.service.impl;


import com.dingyuhang.bean.Topic;
import com.dingyuhang.bean.User;
import com.dingyuhang.mapper.ReplyMapper;
import com.dingyuhang.mapper.TopicMapper;
import com.dingyuhang.service.TopicService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class TopicServiceImpl implements TopicService {
	
	@Autowired
	private TopicMapper topicMapper;
	
	@Autowired
	private ReplyMapper replyMapper;
	
	
	@Override
	public void publishTopic(Topic topic, User user) {
		topic.setUser(user);
		try {
			//添加帖子表
			topicMapper.insertTopic(topic);
			//内容添加到回复表中
			replyMapper.insertReply(topic);
			
		} catch (Exception e) {
			e.printStackTrace(); //一定要输入错误信息
		}
	}
	
	@Override
	public int countTopics(Integer id) {
		
		return topicMapper.countTopics(id);
	}
	
	
	@Override
	public List<Topic> getMyTopics(User user) {
		return topicMapper.getMyTopics(user);
	}
	
	
	@Override
	public Topic topicRelpy(Integer id) {
		return topicMapper.topicRelpy(id);
	}
	
	@Override
	public PageInfo<Topic> selectTopicsByForumId(Integer forumId, Integer pageNum) {
		PageHelper.startPage(pageNum, 5);
		List<Topic>  list = topicMapper.selectTopicsByForumId(forumId);
		return new  PageInfo<>(list);
	}

}
